<?php
class Lists extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        if (file_exists('application/controllers/install.php'))
            redirect('install');
        $this->load->model('works_model');
        $this->load->model('category_model');
        $this->load->library('session');

    }

    function index() {
        $data['title'] = '首页';
        $data['headline'] = '正在进行';

        $data['query'] = $this->works_model->getAll();
        $data['category'] = $this->category_model->getAll();

        $this->load->view('templates/header', $data);
        $this->load->view('lists', $data);
        $this->load->view('templates/footer', $data);
    }

    function search() {
        $data['title'] = '作品列表';
        $id = $this->input->get('id');
        $data['check'] = $this->category_model->select($id);

        if (empty($data['check'])){  // if $id not existed, redirect
            $data['title'] = '出错啦';
            $data['words'] = '查询id错误';
            $this->load->view('templates/header', $data);
            $this->load->view('poster_admin/404', $data);
            $this->load->view('templates/footer', $data);
        } else {
            $data['query'] = $this->works_model->getByCategory($id, $this->works_model->pagination(), 0);
            $data['category'] = $this->category_model->getAll();
            $data['headline'] = $data['check']['category'];
            $this->load->view('templates/header', $data);
            $this->load->view('search', $data);
            $this->load->view('templates/footer', $data);
        }
    }

}